{extend name="index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">青年/妇女创业</div>
    <div class="layui-card-body">
        <div class="layui-form">
            <div class="temTable layui-fluid"><div class="temTable-member-list layui-fluid">
                文章名称：
                <div class="layui-inline">
                    <input type="text" class="layui-input" id="title" name=title" placeholder="模糊查询">
                </div>
                创建时间：
                <div class="layui-inline">
                    <input type="text" class="layui-input" id="addtime" name="addtime" >
                </div>

                <button class="layui-btn search" data-type="reload">搜索</button>
            </div>
            </div>
            <table class="layui-hide" id="table" lay-filter="table"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <a class="layui-btn layui-btn-sm" href="{:url('admin/helpArticle/edits')}">添加青年/妇女创业文章</a>
                </div>
            </script>
            <script type="text/html" id="barTool">

                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="freeze">删除</a>

            </script>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    layui.use(['table','laydate','form'], function() {
        var table = layui.table,
            $ = layui.$
            ,form = layui.form
            ,laydate = layui.laydate;

        //日期时间范围
        laydate.render({
            elem: '#addtime'
            ,type: 'datetime'
            ,range: true
        });

        table.render({
            elem: '#table',
            toolbar: '#toolbarDemo',
            url: '{:url("admin/helpArticle/women")}',
            cols: [
                [
                    { field: 'id', width: 80, title: 'ID',fixed:true},
                    { field: 'title', width: 120, title: '文章标题'},
                    { field: 'type', width: 120, title: '文章类别'},
                    { field: 'info', width: 120, title: '文章内容'},
                    { field: 'create_user', width: 120, title: '发布人' },
                    { field: 'create_time', width: 200, title: '发布时间' },
                    { field: 'sort', title: '排序' },
                    { fixed: 'right', width: 160, title: '操作', toolbar: '#barTool' }
                ]
            ],
            id: 'testReload',
            page: {}
        });

        //监听行工具事件
        table.on('tool(table)', function(obj) {
            var data = obj.data;
            //console.log(obj);
            if (obj.event === 'freeze') {
                layer.confirm('确定删除这个文章？', { icon: 3, title: '提示' }, function(index) {
                    layer.close(index);
                    $.post('{:url("admin/helpArticle/del")}', { 'id': data.id }, function(data) {
                        if (data.code == 1) {
                            if (data.url) {
                                layer.msg(data.msg + ' 页面即将自动跳转~');
                            } else {
                                layer.msg(data.msg);
                            }
                            setTimeout(function() {
                                if (data.url) {
                                    location.href = data.url;
                                } else {
                                    location.reload();
                                }
                            }, 1500);
                        } else {
                            layer.msg(data.msg);
                            setTimeout(function() {
                                if (data.url) {
                                    location.href = data.url;
                                }
                            }, 1500);
                        }

                    });
                });
            }else if (obj.event === 'edit') {
                window.open('{:url("admin/helpArticle/edits")}' + "?id=" + data.id, '_self')
            }
        });

        //监听状态操作
        form.on('switch(isIndex)', function(obj) {
            var _this = $(obj.elem);
            $.post('{:url("admin/helpArticle/setstate")}', { 'id': this.value,'isIndex':obj.elem.checked}, function(data) {
                if (data.code == 1) {
                    layer.msg(data.msg);
                }else{
                    !obj.elem.checked ? _this.prop('checked',true) : _this.removeAttr('checked');
                    form.render('checkbox');
                    layer.msg(data.msg);
                }

            });
        });


        var $ = layui.$, active = {
            reload: function () {
                var title = $("#title").val();
                var time = $("#addtime").val();
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
//                        key: {
                        title: title,
                        time: time
//                        }
                    }
                });
            }
        };
        $('.temTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
{/block}